package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author gsy
 * @date 2021年09月14日14:53
 * Description
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private RabbitService rabbitService;

    @Override
    public PaymentInfo savePaymentInfo(Long orderId, PaymentType paymentType) {
        //根据orderId查询,判断订单是否已经被创建
       PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>().eq("order_id",orderId));
       if (paymentInfo==null){
           //支付表未创建
           //根据orderId 查询对应的订单信息
           OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
           paymentInfo = new PaymentInfo();
           //保存支付信息
           paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
           paymentInfo.setOrderId(orderId);
           paymentInfo.setPaymentType(paymentType.name());
           paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
           paymentInfo.setSubject(orderInfo.getTradeBody());
           paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
           paymentInfo.setCreateTime(new Date());
           paymentInfoMapper.insert(paymentInfo);
       }

       //已经创建
        return paymentInfo;
    }

    @Override
    public PaymentInfo getPaymentInfo(String out_trade_no) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>().eq("out_trade_no",out_trade_no));

    }

    //更新支付表
    @Override
    public void updatePaymentInfo(Map<String, String> paramsMap) {
        //查询 幂等性问题解决
        PaymentInfo paymentInfo = getPaymentInfo(paramsMap.get("out_trade_no"));
        if (paymentInfo!=null&&paymentInfo.getPaymentStatus().equals(PaymentStatus.UNPAID.name())){
             //第一次更新
            //流水编号
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //2021-09-15 10:15:44
            try {
                paymentInfo.setCallbackTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paramsMap.get("notify_time")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //回调内容
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            //更新
            paymentInfoMapper.updateById(paymentInfo);
            //MQ发生消息更新订单表
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY
                    ,MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

        }


    }
}
